package com.appbee.gdata.picasa.server;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Provider;

import com.google.gdata.client.Query;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.media.mediarss.MediaContent;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;



public class PicasaService
{
	private static final Logger log = Logger.getLogger(PicasaService.class.getName());
	
	private static final String baseURL =  "https://picasaweb.google.com/data/feed/api/user/";

	private final Provider<PicasawebService> picasaWebServiceProvider;

	private final PicasaUserAccount userAccount;
	
	@Inject
	public PicasaService(Provider<PicasawebService> picasaWebServiceProvider,
			PicasaUserAccount userAccount)
	{
		this.userAccount = userAccount;
		this.picasaWebServiceProvider = picasaWebServiceProvider;
	}
	
	public List<SimpleAlbum> findAlbums()
	throws Exception
	{
		try
		{
			PicasawebService service = picasaWebServiceProvider.get();
			
			URL feedUrl = new URL(baseURL + userAccount.getUserName());
			
			Query myQuery = new Query(feedUrl);
			myQuery.setStringCustomParameter("kind", "album");
			myQuery.setStringCustomParameter("access", "public");

			UserFeed myUserFeed = service.getFeed(
					myQuery, UserFeed.class);
			List<AlbumEntry> albumEntries = myUserFeed.getAlbumEntries();
			List<SimpleAlbum> list = new ArrayList<SimpleAlbum>(albumEntries.size());
			for (AlbumEntry album : albumEntries) {
				SimpleAlbum a = new SimpleAlbum();
				a.setId(album.getGphotoId());
				a.setTitle(album.getTitle().getPlainText());
				a.setDate(album.getDate());
				a.setDescription(album.getDescription().getPlainText());
				a.setEdited(album.getEdited() == null ? null : new Date(album
						.getEdited().getValue()));
			    List<MediaThumbnail> mediaThumbnails = album.getMediaThumbnails();
			    if(mediaThumbnails.size()>0)
			    {
			    	MediaThumbnail mt = mediaThumbnails.get(0);
			    	a.setThumbnail(mt);
			    }
			    list.add(a);
			}
			return list;
		}
		catch (Exception e)
		{
			log.log(Level.SEVERE, e.getMessage(),e);
			throw e;
		}
	}
	
	public SimplePhoto findPhotoById(String id) throws Exception
	{
		try
		{
			PicasawebService service = picasaWebServiceProvider.get();
			URL feedUrl = new URL(baseURL + userAccount.getUserName()
					+ "/photoid/" + id);
			Query myQuery = new Query(feedUrl);
			myQuery.setMaxResults(1);
			AlbumFeed feed = service.getFeed(myQuery, AlbumFeed.class);

			for (PhotoEntry photo : feed.getPhotoEntries())
			{
				SimplePhoto p = new SimplePhoto();
				List<MediaContent> mediaContents = photo.getMediaContents();
				if (!mediaContents.isEmpty())
					p.setUrl(mediaContents.get(0).getUrl());
				p.setId(photo.getGphotoId());
				p.setTitle(photo.getTitle().getPlainText());
				p.setDescription(photo.getDescription().getPlainText());
				p.setEdited(photo.getEdited() == null ? null : new Date(photo
						.getEdited().getValue()));
				p.setAlbumId(photo.getAlbumId());
				List<MediaThumbnail> mediaThumbnails = photo
						.getMediaThumbnails();
				if (mediaThumbnails.size() > 0)
				{
					MediaThumbnail mt = mediaThumbnails.get(mediaThumbnails
							.size() - 1);
					p.setThumbnail(mt);
				}
				return p;
			}

			return null;
		}
		catch (Exception e)
		{
			log.log(Level.SEVERE, e.getMessage(), e);
			throw e;
		}

	}

	public List<SimplePhoto> findPhotos(String albumid,int start,int length)
	throws Exception
	{
		try
		{
			PicasawebService service = picasaWebServiceProvider.get();
			URL feedUrl = new URL(baseURL + userAccount.getUserName()
					+ "/albumid/" + albumid);

			Query myQuery = new Query(feedUrl);
			myQuery.setStartIndex(start + 1);
			myQuery.setMaxResults(length);

			AlbumFeed feed = service.getFeed(myQuery, AlbumFeed.class);

			List<SimplePhoto> list = new ArrayList<SimplePhoto>();
			for (PhotoEntry photo : feed.getPhotoEntries())
			{
				SimplePhoto p = new SimplePhoto();
				List<MediaContent> mediaContents = photo.getMediaContents();
				if (!mediaContents.isEmpty())
					p.setUrl(mediaContents.get(0).getUrl());
				p.setId(photo.getGphotoId());
				p.setTitle(photo.getTitle().getPlainText());
				p.setDescription(photo.getDescription().getPlainText());
				p.setEdited(photo.getEdited() == null ? null : new Date(photo
						.getEdited().getValue()));
				p.setAlbumId(photo.getAlbumId());
				List<MediaThumbnail> mediaThumbnails = photo
						.getMediaThumbnails();
				if (mediaThumbnails.size() > 0)
				{
					MediaThumbnail mt = mediaThumbnails.get(mediaThumbnails
							.size() - 1);
					p.setThumbnail(mt);
				}
				list.add(p);
			}
			return list;
		}
		catch (Exception e)
		{
			log.log(Level.SEVERE, e.getMessage(), e);
			throw e;
		}
	}
}
